//
//  FillRequirementView.m
//  Fangmm
//
//  Created by 阮琦 on 16/4/5.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "FillRequirementView.h"
#import "WebViewController.h"

#import "ACMacros.h"
#import "Utility.h"
#import "Constants.h"

#import "HttpServer.h"
#import "AppDrawer.h"

#import <TSMessageView.h>

@implementation FillRequirementView {
    FilterRegionsViewController *regionVC;
    FilterSortByViewController *rentVC;
    FilterSortByViewController *orderTimeVC;
    NSString *area;
    NSString *regionParam;
    NSString *districtParam;
    NSString *regionCodeParam;
    NSString *dayParam;
    NSString *priceParam;
    NSArray *rentArray;
    NSArray *orderTimeArray;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
//- (void)drawRect:(CGRect)rect {
//}


- (void)willMoveToSuperview:(UIView *)newSuperview {
    [self configureTableViews];
    area = @"不限";
    regionParam = @"";
    districtParam = @"";
    regionCodeParam = @"3502";
    dayParam = @"7天内";
    priceParam = @"¥800 - ¥1200";
}

- (void)configureTableViews {
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setBounces:false];
    [self.tableView setShowsHorizontalScrollIndicator:false];
    [self.tableView.layer setCornerRadius:5];
    [self.tableView setBackgroundColor:RGBACOLOR(255, 255, 255, 0.9)];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    [cell setBackgroundColor:[UIColor clearColor]];
    [cell.textLabel setFont:[UIFont systemFontOfSize:15]];
    [cell.textLabel setTextColor:[UIColor blackColor]];
    UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 15, 180, 20)];
    [contentLabel setTextColor:RGBACOLOR(119, 119, 119, 1)];
    [contentLabel setFont:[UIFont systemFontOfSize:18]];
    [cell addSubview:contentLabel];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 50, Main_Screen_Width - 60, 0.8)];
    [line setBackgroundColor:RGBACOLOR(255, 255, 255, 0.5)];
    switch (row) {
        case 0:
            [cell addSubview:line];
            [contentLabel setText:area];
            [cell.textLabel setText:@"目标区域"];
            break;
        case 1:
            [cell addSubview:line];
            [contentLabel setText:priceParam];
            [cell.textLabel setText:@"租金预算"];
            break;
        case 2:
            [contentLabel setText:dayParam];
            [cell.textLabel setText:@"预住时间"];
            break;
        default:
            break;
    }
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 2) {
        return 50;
    } else {
        return 51;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    switch (row) {
        case 0:
        {
            if (!regionVC) {
                regionVC = [[FilterRegionsViewController alloc]initWithNibName:@"FilterRegionsViewController" bundle:nil];
                regionVC.delegate = self;
            }
            [self.delegate clickToPresent:regionVC];
        }
            break;
        case 1:
        {
            if (!rentVC) {
                rentVC = [[FilterSortByViewController alloc]initWithNibName:@"FilterSortByViewController" bundle:nil];
                rentVC.delegate = self;
                rentArray = @[@"¥800 - ¥1200", @"¥1200 - ¥1500", @"¥1500 - ¥2000", @"¥2000+"];
                rentVC.sourceDataArray = rentArray;
                
            }
            [self.delegate clickToPresent:rentVC];
        }
            break;
        case 2:
        {
            if (!orderTimeVC) {
                orderTimeVC = [[FilterSortByViewController alloc]initWithNibName:@"FilterSortByViewController" bundle:nil];
                orderTimeVC.delegate = self;
                orderTimeArray = @[@"7天内", @"¥15天内", @"30天内", @"2个月内"];
                orderTimeVC.sourceDataArray = orderTimeArray;
            }
            [self.delegate clickToPresent:orderTimeVC];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - Filter delegates
- (void)doneFilterWithDistrict:(NSString *)district WithRegion:(NSString *)region WithRegionCode:(NSString *)regionCode {
    districtParam = district;
    regionParam = region;
    regionCodeParam = regionCode;
    if ([district isEqual:@"不限"]) {
        area = district;
    } else {
        area = [NSString stringWithFormat:@"%@ - %@",district, region];
    }
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (IBAction)clickToSubscribe:(UIButton *)sender {
    NSString *day = @"7";
    if ([dayParam isEqual:@"7天内"]) {
    } else if ([dayParam isEqual:@"15天内"]) {
        day = @"15";
    } else if ([dayParam isEqual:@"30天内"]) {
        day = @"30";
    } else if ([dayParam isEqual:@"2个月内"]) {
        day = @"60";
    }
    NSDictionary *params = @{@"district":districtParam, @"region":regionParam, @"regionCode":regionCodeParam, @"price":priceParam, @"day":day};
    [HttpServer putRequirementWithParas:params WithSuccessBlock:^(NSDictionary *successDic) {
        Requirement *requirement = [[Requirement alloc]initWithDic:[successDic objectForKey:@"requirement"]];
        [Utility setObject:requirement key:kRequirement];
        [TSMessage showNotificationInViewController:[AppDrawer sharedDrawer] title:@"成功订阅房源!" subtitle:nil type:TSMessageNotificationTypeSuccess duration:2.0 canBeDismissedByUser:true];
    } withFailBlock:^(id failData) {
        [TSMessage showNotificationInViewController:[AppDrawer sharedDrawer] title:@"订阅房源出错啦!" subtitle:@"请稍后尝试重新订阅房源" type:TSMessageNotificationTypeError duration:2.0 canBeDismissedByUser:true];
        NSLog(@"%@",failData);
    }];
}

- (void)didSelectedRow:(NSInteger)row WithItem:(id)item {
    if ([rentArray containsObject:item]) {
        priceParam = item;
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else {
        dayParam = item;
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
}
- (IBAction)clickToPublishHouse:(UIButton *)sender {
    WebViewController *webVC = [[WebViewController alloc]init];
    webVC.titleString = @"发布房源";
    NSLog(@"%@",[Utility getObjectForKey:kUserId]);
    NSString *urlString = [NSString stringWithFormat:@"http://m.fangmm.com/app/client/publish?userid=%@",[Utility getObjectForKey:kUserId]];
    webVC.destinationUrl = urlString;
    UINavigationController *tempNav = (UINavigationController *)[AppDrawer sharedDrawer].centerViewController;
    [tempNav pushViewController:webVC animated:true];
}


@end
